package com.hopu.v1.service.admin.classes.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.mapper.admin.classes.StudentMapper;
import com.hopu.v1.mapper.admin.classes.StudentVoucherMapper;
import com.hopu.v1.pojo.classes.Student;
import com.hopu.v1.pojo.classes.StudentVoucher;
import com.hopu.v1.service.admin.classes.StudentVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author ZhengShuYue
* @description 针对表【t_student_voucher(学生凭证表)】的数据库操作Service实现
* @createDate 2024-09-30 14:36:03
*/
@Service
public class StudentVoucherServiceImpl extends ServiceImpl<StudentVoucherMapper, StudentVoucher>
    implements StudentVoucherService{

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private StudentVoucherMapper studentVoucherMapper;

    /**
     * 通过学号绑定微信
     * @param studentId
     * @param password
     * @return
     */

    @Override
    public String bindStudentId(String studentId ,String password ,String openId) {
        //判断学生是否有微信登陆凭证
        StudentVoucher studentVoucher = studentVoucherMapper.selectOne(
                new LambdaQueryWrapper<StudentVoucher>()
                        .eq(StudentVoucher::getStudentId, studentId)
                        .eq(StudentVoucher::getVoucherType, "wx")
        );
        //若凭证为空，则自动跳转学号绑定页面
        if (studentVoucher == null) {
            //查询学生表里的学号
            Student student = studentMapper.selectOne(
                    new LambdaQueryWrapper<Student>().eq(Student::getStudentId, studentId)
            );

            if (student == null) {
                return "学号不存在，请重新输入";
            }
            //查询凭证表密码
            StudentVoucher voucher = studentVoucherMapper.selectOne(
                    new LambdaQueryWrapper<StudentVoucher>()
                            .eq(StudentVoucher::getStudentId,studentId)
                            .eq(StudentVoucher::getVoucherType,"password")
                            .eq(StudentVoucher::getVoucher,password)
            );
            if (voucher == null) {
                return "密码错误，请重新输入";
            }
            //查询凭证表的openId
            StudentVoucher voucher2 = studentVoucherMapper.selectOne(
                    new LambdaQueryWrapper<StudentVoucher>()
                            .eq(StudentVoucher::getStudentId,studentId)
                            .eq(StudentVoucher::getVoucherType,"wx")
                            .eq(StudentVoucher::getVoucher,openId)
            );
            // 如果 openId 为空，创建新的微信凭证
            if (voucher2 == null) {
                studentVoucher = new StudentVoucher();
                studentVoucher.setStudentId(studentId);
                studentVoucher.setVoucherType("wx");
                studentVoucher.setVoucher(openId);
                studentVoucherMapper.insert(studentVoucher);
                return "该学生绑定成功";
            }

        }
        else {
            return "该学生已绑定微信，请重新登录";
        }
        return "微信绑定成功";
    }

    }





